package com.l4.app.ui;

import java.awt.Color;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

/**
 * 
 * data from an {@link XYDataset}.
 * @author Thusira
 * 
 */
public class Graphs extends ApplicationFrame {


    public static double averageDensity;

    /**
     * Creates a new demo.
     *
     * @param title the frame title.
     */
    public Graphs(String string, List<Double> list, int interval) {
        super(string);
        XYDataset dataset = createDataset(interval, list);
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a sample dataset.
     *
     * @return a sample dataset.
     */
    private static XYDataset createDataset(int interval, List<Double> list) {
        int gap = interval;
        double average = 0.0;
        XYSeries series1 = new XYSeries("Crowd Density");
        XYSeries series2 = new XYSeries("Average Density");
        for (int i = 0; i < list.size(); i++) {
            series1.add(gap, list.get(i));
            //series2.add(gap, 50);
            gap += interval;
            average += list.get(i);
        }
        System.out.println("-------------------------------------" + average / list.size());
        averageDensity=average / list.size();
        for (int i = 0; i < list.size(); i++) {
            series2.add((i + 1) * 5, average / list.size());
        }

//        XYSeries series3 = new XYSeries("Third");
        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series1);
        dataset.addSeries(series2);
//        dataset.addSeries(series3);
        return dataset;
    }

    private static JFreeChart createChart(XYDataset dataset) {
// create the chart...
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Crowd Analysis", // chart title
                "Duration (sec)", // x axis label
                "Crowd Density (%)", // y axis label
                dataset, // data
                PlotOrientation.VERTICAL,
                true, // include legend
                true, // tooltips
                false // urls
                );
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
        chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setShapesVisible(true);
        renderer.setShapesFilled(true);
// change the auto tick unit selection to integer units only...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.
        return chart;
    }

    public Graphs getGraph() {
        this.setBounds(50, 50, 1000, 500);
        return this;
    }
}
